local gamelib = require "club.threecard.gamelib"
local slotlib = require "club.threecard.slotlib"
local components = slotlib.require "components"

local slot_system = slotlib.systems.slot_system
local class = gamelib.entitas.class
local Matcher = gamelib.entitas.Matcher
local GroupEvent = gamelib.entitas.GroupEvent

local auto_system = class("auto_system", slot_system)

function auto_system:initialize()
    self.context.machine:replace(components.auto_component, false, nil)
end

function auto_system:get_trigger()
    return {{Matcher({components.auto_component}), GroupEvent.ADDED}};
end

function auto_system:filter(entity)
    return entity:has(components.auto_component)
end

function auto_system:execute(entitas)
    if entitas:size() ~= 1 then
        gamelib.log.error("数据错误，结果组件不可能>1")
        return;
    end

    entitas:foreach(function(e)
        component = e:get(components.auto_component);
        local is_auto = component.is_auto;

        if not is_auto then
            return;
        end

        if component.auto_func == nil then
            gamelib.log.error("auto_component->auto_func = nil")
            return;
        end

        component.auto_func();
    end)
end

return auto_system